package test.design.chain.mybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Chain {
	
	Object intercept(Object obj, Interceptor interceptor) {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				Object result = interceptor.interceptor(obj, method, args);
				return result;
			}
		});
	}
	
	public static void main(String[] args) {
		Chain chain = new Chain();
		UserService userService = new UserServiceImpl();
		userService = (UserService) chain.intercept(userService, new LogInterceptor());
		userService = (UserService) chain.intercept(userService, new CheckInterceptor());
		
		userService.hello("zhangsan");
	}

}
